Completed
Push — master ( 2040ba...cef9c4 )
by Vitaly
02:22
created

createAPI.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 11
rs 9.4285
nop 2
1
import callAPIMethod from './callAPIMethod';
2
3
import applyMiddleware from './applyMiddleware';
4
5
const createAPI = (resources = {}, middleware = [], APIPrefix = '', fetchOptions = {}) =>
6
7
    Object.keys(resources).reduce( (api, resourceId) => {
8
        api[resourceId] = Object.keys(resources[resourceId].methods).reduce( (resource, method) => {
9
            resource[method] = (params, methodOptions) => {
10
                const apiParams = resources[resourceId].methods[method](params);
11
                const bindedCallAPIMethod = callAPIMethod.bind(null, APIPrefix, fetchOptions, resources[resourceId].prefix);
12
                return applyMiddleware(bindedCallAPIMethod, middleware, methodOptions, apiParams, resourceId, method);
13
            };
14
            return resource;
15
        }, {});
16
        return api;
17
    }, {});
18
19
20
export default createAPI;